package com.example.demo;

import com.pablosz.worm.WormGame;

import android.app.Application;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.preference.PreferenceManager;
import android.util.Log;

public class ApplicationDemo extends Application implements
		OnSharedPreferenceChangeListener {
	static final String TAG = ApplicationDemo.class.getName();

	private WormGame game;
	private SharedPreferences prefs;

	@Override
	public void onCreate() {
		super.onCreate();
		Log.d(TAG, "OnCreate");

		this.prefs = PreferenceManager.getDefaultSharedPreferences(this);
		this.prefs.registerOnSharedPreferenceChangeListener(this);

		initGame();
	}

	private void initGame() {
		this.game = new WormGame(getSizeWorm(), getSteepWorm(), 5, 5);
		
	}

	public WormGame getGame() {
		return game;
	}

	public void setGame(WormGame game) {
		this.game = game;
	}

	public String getUserName() {
		return this.prefs.getString("user", "Ingrese Nombre");
	}

	public Double getHigtScore() {
		return Double.parseDouble(this.prefs.getString("hightscore",
				new String("0")));
	}

	public void setHigtScore(Double score) {
		this.prefs.edit().putString("hightscore", score.toString()).commit();
	}

	@Override
	public void onSharedPreferenceChanged(SharedPreferences prefs, String arg1) {
		Log.d(TAG, "OnSharedPreferenceChanded for Key" + arg1);
		this.prefs = prefs;
		initGame();
	}

	public Integer getSizeWorm() {
		return new Integer(this.prefs.getString("size_worm", new String("20")));
	}
	
	public Integer getSteepWorm() {
		return new Integer(this.prefs.getString("steep_worm", new String("5")));
	}
}
